package com.korustar.autumnal.reflect;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper methods for calculating constructor injection
 * 
 * @author amir
 *
 */
public class ConstructorReflection {
	private static final Logger LOGGER = LoggerFactory.getLogger(ConstructorReflection.class);
	
	public static List<Class<?>> getConstructorArgTypes(Class<?> cls, int argsCount) {
		List<List<Class<?>>> constructorArgss = getConstructorArgs(cls);
		if (constructorArgss!=null) {
			for(List<Class<?>> constructorArgs : constructorArgss) {
				if(constructorArgs.size()==argsCount) {
					LOGGER.info("Found constructor with {} args", argsCount);
					return constructorArgs;
				} else {
					LOGGER.info("Ignoring contstructor with args {} - expecting {}", constructorArgs.size(), argsCount);
				}
			}
		}
		return null;
	}
	
	public static List<List<Class<?>>> getConstructorArgs(Class<?> cls) {
		Constructor<?>[] constructors = cls.getConstructors();
		List<List<Class<?>>> ret= new ArrayList<List<Class<?>>>();
		for(Constructor<?> c : constructors) {
			Class<?>[] parameterTypes = c.getParameterTypes();
			List<Class<?>> asList = Arrays.asList(parameterTypes);
			ret.add(asList);
		}
		return ret;
	}
}
